<?php
	//path=fore/1.html
	//若远程文件夹没有创建要先同步文件夹
	function rsync($path, $is_need_clear_cache=true )
	{
		$private_key_file_path = '/data/web/v.169.net/sinotone';
		$ssh_cmd = '/usr/bin/ssh';

		if( empty($path) || substr($path,0,1) == '/' )
		{
			return array('status'=>1, 'msg'=>'[1]参数错误.');
		}

		$token_arr = explode('/', $path);
		$token = $token_arr[0];
		if($token != 'fore' && $token != 'lib' && $token != 'tpl' && $token != 'admin'  && $token != 'union'  && $token != 'fms' && $token != 'maintenance' )
		{
			return array('status'=>1, 'msg'=>'[2]此文件不充许同步.');
		}

		if($token == 'lib')
		{
			if( count($token_arr) == 2 && $token_arr[1] == 'config.php' )
			{
				return array('status'=>1, 'msg'=>'[3]此文件不充许同步.');
			}
		}

		if( $token == 'fms' )
		{
			$dpath = substr($path, strpos($path, '/' ) + 1 );
			//192.168.1.103 -> 119.145.130.115 (v.169.net Hall)
			//192.168.1.26 -> 119.145.130.96
			//192.168.1.44 -> 119.145.130.114

			//192.168.1.106 -> 119.145.130.92 (yochatHall)	可以不用同步,没有此文件夹.yochat与youmiss共用视频服务器
			//192.168.1.45 -> 撤掉的1G内存机器
			$fms_remote_hostname = array('192.168.1.103', '192.168.1.26', '192.168.1.106');
			$fms_remote_dst = array('/data/soft/fms/applications/' . $dpath, '/data/soft/fms/applications/' . $dpath, '/server/fms3/applications/' . $dpath);
			$fms_remote_ssh_port = array(22, 22, 22);
			$fms_local_src = '/data/soft/fms3/applications/' . $dpath;

			foreach($fms_remote_hostname as $idx=>$remote)
			{
				if( $remote == '' ) continue;
				if( is_file($fms_local_src) ||  is_dir($fms_local_src) )
				{
					$rsync_cmd = "/usr/bin/rsync --exclude=test --exclude=.svn -aue \"/usr/bin/ssh -l swingcoder -i $private_key_file_path -p " . $fms_remote_ssh_port[$idx] . "\" $fms_local_src " . $fms_remote_hostname[$idx] . ":" . $fms_remote_dst[$idx];
					passthru($rsync_cmd);
					//echo $rsync_cmd . '<br />';
				}
				else
				{
					return array('status'=>1, 'msg'=>'[4]fms文件或目录不存在. ' . $local_src);
				}
			}
		}
		else
		{
			$dpath = substr($path, 0, strrpos($path, '/' ) + 1 );
			$remote_hostname = array('192.168.1.55', '192.168.1.56', '192.168.1.44', '192.168.1.232', '192.168.1.20', '192.168.1.21', '192.168.1.120');
			$remote_dst = array('/data/v.169.net/' . $dpath, '/data/v.169.net/' . $dpath, '/data/web/v.169.net/' . $dpath, '/data/v.169.net/' . $dpath, '/data/v.169.net/' . $dpath, '/data/v.169.net/' . $dpath, '/data/v.169.net/' . $dpath);
			$remote_ssh_port = array(22, 22, 22, 22, 22, 22, 22);
			//因rsync同步时不会自动创建文件夹,不能指定目的地为文件,需要为文件夹
			$exclude_pattern = array('--exclude=admin' , '--exclude=admin', '', '--exclude=admin', '--exclude=admin', '--exclude=admin', '--exclude=admin');
			$local_src = config('dir.root') . $path;

			//后台上传文件需要同步,不能限制
			//if( $_SERVER['SERVER_NAME'] != 'admin.v2.169.net' )
			//{
			//	return array('status'=>1, 'msg'=>'[5]该文件不允许在此域名下同步. ' . $local_src);
			//}
			foreach($remote_hostname as $idx=>$remote)
			{
				if( $remote == '' ) continue;
				if( is_file($local_src) ||  is_dir($local_src) )
				{
					$rsync_cmd = "/usr/bin/rsync --exclude=test --exclude=.svn " . $exclude_pattern[$idx] . " -aue \"/usr/bin/ssh -l swingcoder -i $private_key_file_path -p " . $remote_ssh_port[$idx] . "\" $local_src " . $remote_hostname[$idx] . ":" . $remote_dst[$idx];

					//同步文件夹要用*
					// /usr/bin/scp -r -i /data/web/v.169.net/sinotone /server/httpd/htdocs/v.169.net/tpl/* swingcoder@192.168.1.44:/data/web/v.169.net/tpl/
					$scp_cmd = "/usr/bin/scp -r -i /data/web/v.169.net/sinotone " . $local_src . ( is_dir( $local_src ) ? "*" : "" ) ." swingcoder@192.168.1.44:" . $remote_dst[$idx];

					//非192.168.1.44 IP不同步admin下的内容
					if( $token == 'admin' && $remote != $remote_hostname[2] )
					{
						continue;
					}

					//特殊原因,同步到192.168.1.44要用scp命令
					if( $remote_hostname[$idx] == '192.168.1.44' )
					{
						passthru( $scp_cmd );
						//echo $scp_cmd . '<br/>';
					}
					else
					{
						passthru( $rsync_cmd );
						//echo $rsync_cmd . '<br/>';
					}
				}
				else
				{
					return array('status'=>1, 'msg'=>'[4]文件或目录不存在. ' . $local_src);
				}
			}
		}

		return array('status'=>0, 'msg'=>'同步成功. ');
	}

	function clear_cache_cmd($token, $suffix)
	{
		return true;

		$damain_map = array('tpl'=>'http://tpl.youmiss.net', 'fore'=>'http://www.yomiss.net' );
		$squidclient = '/opt/squid/bin/squidclient -h 192.168.1.254 -p 80 -m purge ' . $damain_map[$token] . $suffix;
		ob_start();
		passthru($squidclient);
		$out = ob_get_contents();
		ob_end_clean();
		eregi('HTTP/1.0 ([0-9]{3})', $out, $code);

		if( $code[1] == '200' )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
?>